<template>
  <a-modal :title="title" style="top: 100px;" :visible="true" width="1300px" @cancel="handleCancel">
    <div class="tips">
    </div>
    <a-table class="product-table" :rowKey="row => row.id" :dataSource="list" bordered :pagination="false" size="small">

      <a-table-column title="序号" width="20px">
        <template slot-scope="text, record, index">
          <div>{{ index+1 }}</div>
        </template>
      </a-table-column>

      <a-table-column title="图片" key="thumb" width="120px">
        <template slot-scope="row">
          <div style="display: flex; align-items: center;">
            <img :src="row.thumb" class="thumb" style="margin-right: 10px;" />
            <div>
              {{row.title}} <br/>
              {{row.number}}
            </div>
          </div>
        </template>
      </a-table-column>
      <a-table-column title="?等奖" width="80px">
        <template slot-scope="row">
          <div>
            {{row.ranking}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="奖励人数" width="80px">
        <template slot-scope="row">
          <div>
            {{row.ranking_num}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="回收价" width="100px">
        <template slot-scope="row">
          <div>
            <PriceDisplay :info="row"></PriceDisplay>
          </div>
        </template>
      </a-table-column>
      <a-table-column title="显示零售价" width="100px">
        <template slot-scope="row">
          <div>
            <PriceDisplay :info="row" prefix="display_"></PriceDisplay>
          </div>
        </template>
      </a-table-column>
      <a-table-column title="SKU类型" width="120px">
        <template slot-scope="row">
          <ISkuType :info="row"></ISkuType>
        </template>
      </a-table-column>
      
      <a-table-column title="操作" width="120px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEdit(row)">编辑</a>
          <a-divider type="vertical" />
           <a href="javascript:;" @click="handleDelete(row)">删除</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <a-button type="primary" class="add" @click="handleCreate">
          新增一款
        </a-button>
        <div style="flex-grow: 1;"></div>

        <a-button key="back" @click="handleCancel">关闭</a-button>
        <!-- <a-button key="submit" type="primary" @click="handleSave">
          保存
        </a-button> -->
      </div>
    </template>
  </a-modal>
</template>
<script>
  import UploadImages from '@/components/UploadImages/UploadImages'
  import iSwitch from '@/components/ISwitch/ISwitch'
  import ISkuType from '@/components/ISkuType/ISkuType'
  export default {
    components: {
      UploadImages,
      iSwitch,
      ISkuType
    },
    props: {
      info: {
        type: Object,
        default () {
          return {}
        }
      },
    },
    data() {
      return {
        list: [],
        listWeight: '',
        frame: [{
            title: 'SKU类型',
            key: 'sku_type',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [{
                title: '无绑定',
                value: 1
              },
              {
                title: '绑定商城中的商品',
                value: 2
              },
              {
                title: '平台虚拟资产',
                value: 3
              }
            ],
            tips: ''
          },
          {
            title: '绑定商品',
            key: 'product_id',
            type: 'product_id',
            auto_fill_fields: {
              'title': 'title',
              'thumb': 'thumb',
              'number': 'number',
              'money_price': ['money_price','display_money_price'],
              'score_price': 'score_price'
            },
            where: {
              sku_type: 2
            }
          },
          {
            title: '绑定虚拟资产',
            key: 'assets',
            type: 'assets',
            where: {
              sku_type: 3
            }
          },
          {
            title: '奖品名称',
            key: 'title',
            required: true,
            placeholder: '如"积分礼包1"'
          },
          {
            title: '编号',
            key: 'number',
            placeholder: '可填写编号方便在订单中对应货物'
            // required: true,
          },
          {
            title: '封面',
            key: 'thumb',
            type: 'image'
          },
          {
            title: '?等奖',
            key: 'ranking',
            required: true,
            placeholder: '如"1"'
          },
          {
            title: '奖励人数',
            key: 'ranking_num',
            required: true,
            placeholder: '如过需要奖励4-7名为4等奖,则奖励名次设4奖励人数设3'
          },
          {
            title: '回收价',
            key_prefix: '',
            type: 'mix_price',
            tips: '价值用于返售时作为返售原价进行折扣计算',
            newline: true,
            required: true,
          },
          {
            title: '显示零售价',
            key: 'display_money_price',
            type: 'price',
            tips: '仅用于显示',
            newline: true,
            required: true,
          },
          {
            title: '奖品开启返售',
            key: 'is_return_saleable',
            type: 'boolean',
            newline: true,
            tips: '开启后领主奖品可返售'
          },
          {
            title: '奖品详情图',
            key: 'detail_images',
            type: 'images',
            newline: true,
            sizeLimit: 400,
            tips: '显示在用户的物品详情中。 不上传则不显示。 <br/> 长图请切成多张小图上传'
            // tips: '不设置则没有连'
            // required: true
          }
        ]
      }
    },
    computed: {
      title() {
        return '编辑领主奖品'
      }
    },
    watch: {
      uuid() {
        this.initList()
      },
    },
    created() {
      this.initList()
    },
    methods: {
      statusChange(e, row) {
        this.$http.put(`/fudai/lord-skus/${row.uuid}`, {
          type: 'update',
          attributes: {
             status: e
          }
        }).then(res => {
          this.initList()
          // this.$iForm.hide()
        })
      },
      initList() {
        this.$http.get(`/fudais/${this.info.uuid}/lord-skus`, {
          params: {
            lord_id: this.info.id
          }
        }).then(res => {
          this.list = res.data.list
        })
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增领主奖品',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/fudais/${this.info.uuid}/lord-skus`, {
              ...data,
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEdit(row) {
        this.$iForm.show({
          title: '编辑领主奖品',
          frame: this.frame,
          form: row,
          success: (data) => {
            this.$http.put(`/fudai/lord-skus/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleCancel() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleSave() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {

            this.$http.delete(`/fudai/lord-skus/${row.uuid}`).then(res => {
              this.$message.success('删除成功')
              this.initList()
            })

          }
        })


      }
    }
  }
</script>
<style lang="less" scoped>
  .thumb {
    width: 60px;
    // height: 60px;
    border-radius: 6px;
  }

  .btn-c {
    display: flex;
    // margin-top: 30px;
    padding: 0rpx 30rpx;

    .add {}

    .save {
      margin-right: 20px;
    }

    .cancel {}
  }

  .tips {
    color: gray;
    font-size: 80%;
    margin-bottom: 10px;
  }

  a-input {
    display: inline;
  }
</style>
